//
//  ActionModel.m
//  
//  jhf
//
//  Created by 计海峰 on 2020/3/17.
//  Copyright © 2020 计海峰. All rights reserved.
//

#import "ActionModel.h"

@implementation ActionModel

+ (ActionModel *)initBindKey:(NSString *)bindKey {
    ActionModel *action = ActionModel.new;
    action.bindKey = bindKey;
    NSString *key = [NSString stringWithFormat:@"ActionModel_%f_%u", NSDate.new.timeIntervalSince1970, arc4random()];
    action.key = key;
    return action;
}

- (void)configMethod:(NSString *)method any:(_Nullable id)any{
    self.method = method;
    self.any = any;
}

- (void)run:(NSString *)method any:(_Nullable id)any {
    [self configMethod:method any:any];
    [[NSNotificationCenter defaultCenter] postNotificationName:self.bindKey object:self];
}

// 对应枚举 action的key 或者 action本身
- (void)runService:(kTableServiceType)service any:(_Nullable id)any; {
    [self run:[ActionModel kTableServiceMethod:service] any:any];
}

- (BOOL)runTarget:(NSObject *)obj {
    NSString *realMethod = self.method;
    if (!self.any) {
        self.any = self.bindKey;
    }
    if (![realMethod hasSuffix:@":"]) {
        realMethod = [NSString stringWithFormat:@"%@:", realMethod];
    }
    SEL sel = NSSelectorFromString(realMethod);
    if ([obj respondsToSelector:sel]) {
        [obj performSelectorOnMainThread:sel withObject:self.any waitUntilDone:false];
        return true;
    }
    return false;
}

+ (NSString *)kTableServiceMethod:(kTableServiceType)type{
    NSString *method = @"";
    switch (type) {
        case ReloadList:
            method = @"reloadList";
            break;
        case ReloadSection:
            method = @"reloadSection";
            break;
        case ReloadRow:
            method = @"reloadRow";
            break;
        case RemoveRow:
            method = @"removeRow";
            break;
        case RemoveSection:
            method = @"removeSection";
            break;
        case HiddenKeyboard:
            method = @"hiddenKeyboard";
            break;
        case ReloadData:
            method = @"reloadData";
            break;
        case ReForRows:
            method = @"reForRows";
             break;
        case RefreshRowHeight:
            method = @"refreshRowHeight";
            break;
        case RefreshUpdates:
            method = @"refreshUpdates";
            break;
        default:
            break;
    }
    return method;
}

@end
